﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace carsitem.Supplier
{
    public partial class SupplierEntryForm : Form
    {
        //皮肤组件声明
        Sunisoft.IrisSkin.SkinEngine se = null;
        string SkinName = "SteelBlack";

        public SupplierEntryForm()
        {
            InitializeComponent();

            //皮肤组件引用
            se = new Sunisoft.IrisSkin.SkinEngine();
            se.SkinAllForm = true;
            se.SkinFile = "skin/" + SkinName + ".ssk";
        }

        //退出按钮
        private void Exit_Click(object sender, EventArgs e)
        {
            carsitem.MainInterface.MainInterfaceForm mainInterfaceForm = new carsitem.MainInterface.MainInterfaceForm();//把form1传给form2
            mainInterfaceForm.Show();
            this.Hide();//把自己隐藏了
        }

        //添加按钮
        private void Add_Click(object sender, EventArgs e)
        {
            try// 捕捉全空文本框异常
            {
                //sql添加语句
                string sql = string.Format("insert into Supplier(SupplierName,PhoneNumber,TheContact,Email,BankAccount,SupplierAddress) values('{0}',{1},'{2}',{3},{4},{5})", this.textSupplierName.Text, this.textPhoneNumber.Text, this.textTheContact.Text, this.textEmail.Text, this.textBankAccount.Text, this.textSupplierAddress.Text);

                //判断是否成功
                if (DBHelper.Instance().ExcuteSql(sql))
                {
                    MessageBox.Show("添加成功！");
                }
                else
                {
                    MessageBox.Show("添加失败！");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("请输入要添加的数据！");//文本框中无数据弹出
            }
            
        }

        private void Reset_Click(object sender, EventArgs e)
        {
            this.textSupplierNumber.Clear();
            this.textSupplierName.Clear();
            this.textPhoneNumber.Clear();
            this.textTheContact.Clear();
            this.textEmail.Clear();
            this.textBankAccount.Clear();
            this.textSupplierAddress.Clear();
        }

        //数字验证
        public bool IsNumberic(string oText)
        {
            string pattern = @"^\d+(\.\d)?$";
            if (oText != "")
            {
                if (!Regex.IsMatch(oText, pattern))
                    MessageBox.Show("请输入数字！");
            }
            return true;
        }

        //汉字验证
        public bool IsChineseCharacters(string oText)
        {
            string text = @"[\u4E00-\u9FA5]+$";
            if (oText != "")
            {
                if (!Regex.IsMatch(oText.ToString(), text))
                    MessageBox.Show("请输入汉字！");
            }
            return true;
        }

        //非空验证
        public bool Null(string oText)
        {
            if (oText == "")
            {
                MessageBox.Show("必须填写！");
            }
            return true;
        }

        private void textSupplierNumber_Leave(object sender, EventArgs e)//供商编号
        {
            string oText = textSupplierNumber.Text.Trim();
            IsNumberic(oText);
            Null(oText);
        }

        private void textSupplierName_Leave(object sender, EventArgs e)//供商名称
        {
            string oText = textSupplierName.Text.Trim();
            IsChineseCharacters(oText);
            Null(oText);
        }

        private void textPhoneNumber_Leave(object sender, EventArgs e)//手机号码
        {
            string oText = textPhoneNumber.Text.Trim();
            IsNumberic(oText);
            Null(oText);
            if (textPhoneNumber.Text.Length != 11)
            {
                MessageBox.Show("手机号码为11位！");
            }
        }

        private void textTheContact_Leave(object sender, EventArgs e)//联系人
        {
            string oText = textTheContact.Text.Trim();
            IsChineseCharacters(oText);
            Null(oText);
        }

        private void textBankAccount_Leave(object sender, EventArgs e)//银行账号
        {
            string oText = textBankAccount.Text.Trim();
            string text = @"[\u4E00-\u9FA5]+$";
            if (oText != "")
            {
                if (Regex.IsMatch(oText.ToString(), text))
                    MessageBox.Show("出现汉字！");
            }
            Null(oText);
        }
    }
}
